package com.ls.mysql.quartz;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ls.mysql.quartz.entity.Quartz;
import com.ls.mysql.quartz.entity.ScheduleJob;
import com.ls.mysql.quartz.service.SchedulerService;
import com.ls.mysql.util.DateUtil;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.List;

/**
 * @Author Maple.Chen
 * @Description 管理任务调度的控制器
 * @Date 20:55 2020/5/28
 **/
@Controller
public class QuartzController {

    @Autowired
    private SchedulerService schedulerService;

    /*
    清除所有任务触发：
    delete from qrtz_cron_triggers;
    delete from qrtz_simple_triggers;
    delete from qrtz_triggers;
    delete from qrtz_job_details;
    delete from qrtz_locks;
    delete from qrtz_scheduler_state;
    delete from qrtz_fired_triggers;
     */

    @ResponseBody
    @RequestMapping(value = "quartz/add1")
    public JSONObject addJob(HttpServletRequest request, @RequestParam(value = "classPath", required = false) String classPath) throws ParseException, SchedulerException, ClassNotFoundException {
        JSONObject json = new JSONObject();
        Quartz q1 = new Quartz();
        String cron = request.getParameter("cron") == null ? null : request.getParameter("cron");
        String jobName = request.getParameter("jobName") == null ? null : request.getParameter("jobName");
        String jobGroup = request.getParameter("jobGroup") == null ? null : request.getParameter("jobGroup");
        q1.setCronExpression(cron);
        q1.setJobName(jobName);
        q1.setJobGroup(jobGroup);
        /*
        注，同一分组的任务，会按该分组的第一个任务所在的执行类执行
         */
        q1.setStartTime(DateUtil.getCurrentDateTime());
        q1.setClassPath(classPath);
        String result = schedulerService.createJob(q1);
        json.put("result", result);
        return json;
    }

    @ResponseBody
    @RequestMapping(value = "quartz/retrieve1")
    public JSONObject retrieveJob(@RequestParam("jobname") String jobName, @RequestParam("jobgroup") String jobGroup) throws SchedulerException {
        JSONObject json = new JSONObject();
        String status = schedulerService.retrieveJobState(jobName, jobGroup);
        json.put("job status", status);
        return json;
    }

    @ResponseBody
    @RequestMapping(value = "quartz/pause1")
    public JSONObject pauseJob(@RequestParam("jobname") String jobName, @RequestParam("jobgroup") String jobGroup) throws SchedulerException {
        JSONObject json = new JSONObject();
        boolean result = schedulerService.pauseJob(jobName, jobGroup);
        json.put("暂停operate result", result);
        return json;
    }


    @ResponseBody
    @RequestMapping(value = "quartz/resume1")
    public JSONObject resumeJob(@RequestParam("jobname") String jobName, @RequestParam("jobgroup") String jobGroup) throws SchedulerException {
        JSONObject json = new JSONObject();
        boolean result = schedulerService.resumeJob(jobName, jobGroup);
        json.put("恢复operate result", result);
        return json;
    }

    @ResponseBody
    @RequestMapping(value = "quartz/retrieveAll1")
    public JSONObject retrieveAllJob() throws SchedulerException {
        JSONObject json = new JSONObject();
        // 获取所有的任务
        List<ScheduleJob> allJob = schedulerService.getAllJob();
        if (allJob.size() > 0) {
            JSONArray ja = new JSONArray();
            for (ScheduleJob j : allJob) {
                ja.add(j);
            }
            json.put("All Job", ja);
        }
        return json;
    }

}
